﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Topshelf;
using System.Timers;

namespace ConsoleApp
{
    public static class demo_topshelf
    {
        public static void demo()
        {
            HostFactory.Run(x =>
            {
                x.Service<TownCrier>(s =>                        
                {
                    s.ConstructUsing(name => new TownCrier());    
                    s.WhenStarted(tc => tc.Start());             
                    s.WhenStopped(tc => tc.Stop());               
                });

                x.RunAsLocalSystem();

                x.SetDescription("Sample Topshelf Host");        
                x.SetDisplayName("Stuff");                       
                x.SetServiceName("Stuff");  
            });
        }
    }

    public class TownCrier
    {
        readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        readonly Timer _timer;

        public TownCrier()
        {
            _timer = new Timer(1000 * 60) { AutoReset = true };
            _timer.Elapsed += (sender, eventArgs) =>
            {
                //Console.WriteLine("It is {0} and all is well", DateTime.Now);
                log.Info(string.Format("It is {0} and all is well", DateTime.Now));
            };
        }
        public void Start() { _timer.Start(); }
        public void Stop() { _timer.Stop(); }
    }
}